/**
 * Created by IntelliJ IDEA.
 * User: Tomek
 * Date: 06.10.12
 * Time: 17:41
 * To change this template use File | Settings | File Templates.
 */
package zakladki.renderers {
import AS3.helpers.FlagImageHelper;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Rectangle;

import flash.utils.Dictionary;

import mx.charts.ChartItem;

import mx.charts.ChartItem;

import mx.charts.ChartItem;
import mx.charts.chartClasses.GraphicsUtilities;

import mx.charts.chartClasses.IChartElement;
import mx.charts.series.items.PlotSeriesItem;
import mx.controls.Alert;
import mx.controls.Image;
import mx.core.FlexGlobals;
import mx.core.IDataRenderer;
import mx.graphics.IFill;
import mx.graphics.IStroke;
import mx.graphics.SolidColor;
import mx.skins.ProgrammaticSkin;
import mx.utils.ColorUtil;

import spark.primitives.Graphic;

import zakladki.decorators.PlotItemDecorator;

public class GDPEnergyItemRenderer extends ProgrammaticSkin implements IDataRenderer{


    private static var rcFill:Rectangle = new Rectangle();

    private var plotItem:Object;
    private var lastClickedCountry:String = null;


    private var mainApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    public function GDPEnergyItemRenderer()  {

    }

    public function get data():Object {
        return plotItem;
    }

    public function set data(value:Object):void {
        plotItem = value;
    }

    override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void {
        try {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            var fill:IFill;
            var state:String = "";

            if (plotItem is ChartItem && plotItem.hasOwnProperty('fill')) {
                fill = plotItem.fill;
                state = plotItem.currentState;

            }
            else {
                fill = GraphicsUtilities.fillFromStyle(getStyle('fill'));
            }

            var showFlag:Boolean = false;
            var ctrName:String = null;
            var region:String = null;

            if(plotItem.hasOwnProperty('item')){
                var myItem:Object = plotItem.item;
                if (myItem.hasOwnProperty('flag')) {
                    showFlag = myItem.flag as Boolean;
                }
                if(myItem.hasOwnProperty('countryName')){
                    ctrName = myItem.countryName;
                }
                if(myItem.hasOwnProperty('region')){
                    region = myItem.region;
                }
            }

            var element:IChartElement = plotItem.element;

            var color:uint;
            var adjustedRadius:Number = 0;

            var selected:Boolean = false;

            if (showFlag && ctrName != null) {
                switch (state) {
                    case ChartItem.FOCUSED:
                    case ChartItem.ROLLOVER:
                        if (styleManager.isValidStyleValue(getStyle('itemRollOverColor'))) {
                            color = getStyle('itemRollOverColor');
                        }
                        else {
                            color = ColorUtil.adjustBrightness2(GraphicsUtilities.colorFromFill(fill), -20);
                        }
                        fill = new SolidColor(color);
                        adjustedRadius = getStyle('adjustedRadius');
                        if (!adjustedRadius) {
                            adjustedRadius = 0;
                        }
                        break;
                    case ChartItem.DISABLED:
                        if (styleManager.isValidStyleValue(getStyle('itemDisabledColor'))) {
                            color = getStyle('itemDisabledColor');
                        }
                        else {
                            color = ColorUtil.adjustBrightness2(GraphicsUtilities.colorFromFill(fill), 20);
                        }
                        fill = new SolidColor(GraphicsUtilities.colorFromFill(color));
                        break;
                    case ChartItem.FOCUSEDSELECTED:
                        break;
                    case ChartItem.SELECTED:
                        selected = true;
                        plotItem.currentState = ChartItem.NONE;
                        break;
                }
            }


            //  fill = new SolidColor(fillColorRed);
            var stroke:IStroke = getStyle("stroke");

            var w:Number = stroke ? stroke.weight / 2 : 0;

            //rcFill.right = unscaledWidth;
            //rcFill.bottom = unscaledHeight;

            var g:Graphics = graphics;
            g.clear();
            if (stroke) {
                stroke.apply(g, null, null);
            }
            if (fill) {
                fill.begin(g, rcFill, null);
            }

            var ellipseX:Number = w - adjustedRadius;
            var ellipseY:Number = w - adjustedRadius;
            var ellipseWidth:Number = unscaledWidth - 2 * w + adjustedRadius * 2;
            var ellipseHeight:Number = unscaledHeight - 2 * w + adjustedRadius * 2;

            g.drawEllipse(ellipseX,ellipseY,ellipseWidth,ellipseHeight);

            if(showFlag && ctrName != null && region != null){
                try {
                    var objVar1:Object = {nazwa_panstwa:ctrName ,country:"" , nazwa:"",source:null};
                    FlagImageHelper.fillFlagImageAndNameByNativeName(objVar1);
                    var flagBitmapData:BitmapData = objVar1.source;

                    var scale:Number = 1;
                    var matrix:Matrix = new Matrix();
                    matrix.scale(scale, scale);

                    var scaledBitmap:BitmapData = new BitmapData(flagBitmapData.width * scale, flagBitmapData.height * scale);
                    scaledBitmap.draw(flagBitmapData, matrix);
                    flagBitmapData.dispose();

                    var translatedX:Number = ellipseX + ellipseHeight - 1;
                    var translatedY:Number = ellipseY + ellipseWidth + 1;

                    matrix.translate(translatedX,translatedY);
                    g.beginBitmapFill(scaledBitmap, matrix, false, false);
                    g.drawRect(translatedX,translatedY,scaledBitmap.width, scaledBitmap.height);

                    mainApp.chooseCountryPanel.fillPlotFlagCountryMap(region,ctrName,new PlotItemDecorator(plotItem,new Rectangle(plotItem.x,plotItem.y,scaledBitmap.width, scaledBitmap.height),true));
                } catch(e:Error) {
                    Alert.show("error plot flag for ctrName: " + ctrName +  " " + e.message);
                }
            }
            else if(ctrName != null && region != null){
                mainApp.chooseCountryPanel.fillPlotFlagCountryMap(region,ctrName,new PlotItemDecorator(plotItem,new Rectangle(plotItem.x,plotItem.y,ellipseWidth,ellipseHeight),false));
            }

            if (fill) {
                fill.end(g);
            }



        } catch(e:Error) {
            Alert.show("Error plot" + e.getStackTrace() + " " + e.message);
        }
    }

}
}
